package com.example.projectcode.controller.Student;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.projectcode.model.*;
import com.example.projectcode.result.Result;
import com.example.projectcode.service.*;
import com.example.projectcode.vo.TestCpstuanVo;
import com.example.projectcode.vo.TestCstuanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "学生模拟考试接口")
@RestController
@RequestMapping(value = "/user/practicetest")
public class PracticeTestController {

     @Autowired
     private TestService testService;

     @Autowired
     private TestchoiceService testchoiceService;

     @Autowired
     private TestcomprehensiveService testcomprehensiveService;

     @Autowired
     private ChoiceService choiceService;

     @Autowired
     private ComprehensiveService comprehensiveService;

     @Autowired
     private TestCstuanService testCstuanService;

      @Autowired
      private TestCpstuanService testCpstuanService;

      @Autowired
      private TestRecordService testRecordService;

    @ApiOperation("查看试卷条目")
    @GetMapping(value = "/viewtest")
    public Result viewtest(){

        List<Test> testList = testService.selectAllTests();
       return Result.ok(testList);
    }


    @ApiOperation("开始考试,根据试卷id查找试卷题目")
    @GetMapping(value = "/starttest/{testId}")
    public Result starttest(@PathVariable Integer testId)
    {
         List<Testchoice> testchoiceList = testchoiceService.selectChoiceByTestId(testId);
         List<Testcomprehensive> testcomprehensiveList = testcomprehensiveService.selectComprehensiveByTestId(testId);

         List<Choice> choiceList = new ArrayList<>();
         for (Testchoice testchoice : testchoiceList){
             Choice choice = new Choice();
             choice = choiceService.selectChoiceByCId(testchoice.getChoiceId());
             choiceList.add(choice);
         }
         List<Comprehensive> comprehensiveList = new ArrayList<>();
         for (Testcomprehensive testcomprehensive: testcomprehensiveList){
             Comprehensive comprehensive = new Comprehensive();
             comprehensive = comprehensiveService.selectComprehensiveByCpId(testcomprehensive.getCpId());
             comprehensiveList.add(comprehensive);
         }
         Map<String,Object> map = new HashMap<>();
         map.put("试卷id",testId);
         map.put("试卷选择题",choiceList);
         map.put("试卷综合题",comprehensiveList);
         return Result.ok(map);


    }


    @ApiOperation("考试选择题提交")
    @PostMapping("/submitChoice")
    public Result submitchoice(@RequestBody TestCstuanVo testCstuanVo){
        int result = testCstuanService.insertChoice(testCstuanVo);
        if (result>0){
            return Result.ok().message("作答成功");
        }else {
            return Result.fail().message("作答失败");
        }
    }

    @ApiOperation("考试综合题提交")
    @PostMapping("/submitComprehensuve")
    public Result submitComprehensive(@RequestBody TestCpstuanVo testCpstuanVo){
              int result = testCpstuanService.insertComprehensive(testCpstuanVo);
        if (result>0){
            return Result.ok().message("作答成功");
        }else {
            return Result.fail().message("作答失败");
        }
    }


    @ApiOperation("学生提交试卷，试卷保存到考试记录表")
    @GetMapping("/submittest/{testId}/{stuId}")
    public Result submittest(@PathVariable Integer testId,@PathVariable Integer stuId){
          int result = testRecordService.insertTestRecord(testId,stuId);
          int result2 = testService.deletetestById(testId);
        if (result>0&&result2>0){
            return Result.ok().message("提交成功");
        }else {
            return Result.fail().message("提交失败");
        }
    }


    /*------------------------------------------新加----------------------------------*/

    @ApiOperation("分页查询所有试卷条目")
    @GetMapping("queryAllTest/{page}/{size}")
    public Result queryAllTest(@PathVariable long page, @PathVariable long size){
        Page<Test> testPage = new Page<>(page,size);
        IPage<Test> testIPage = testService.page(testPage,null);
        return Result.ok(testIPage);
    }




}
